package cc.ucanuup.config.atom;

import java.sql.SQLException;
import java.util.HashMap;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;

import cc.ucanuup.config.atom.dbsource.OtherDatasourceProperties;

@Configuration
@DependsOn("transactionManager")
@EnableJpaRepositories(basePackages = "cc.ucanuup.service.demo.datamanager", entityManagerFactoryRef = "t100EntityManager", transactionManagerRef = "transactionManager")
@EnableConfigurationProperties(OtherDatasourceProperties.class)
public class OtherConfig {

	@Autowired
	private JpaVendorAdapter jpaVendorAdapter;

	@Autowired
	private OtherDatasourceProperties otherDatasourceProperties;

	@Bean(name = "t100DataSource", initMethod = "init", destroyMethod = "close")
	@Qualifier("t100DataSource")
	public DataSource t100DataSource() throws SQLException {
		System.out.println("mysql Blog:"+otherDatasourceProperties.getUrl());
		System.out.println("mysql Blog:"+otherDatasourceProperties.getUsername());
		System.out.println("mysql Blog:"+otherDatasourceProperties.getPassword());
		MysqlXADataSource oracleXADataSource = new MysqlXADataSource();
		oracleXADataSource.setURL(otherDatasourceProperties.getUrl());
		oracleXADataSource.setUser(otherDatasourceProperties.getUsername());
		oracleXADataSource.setPassword(otherDatasourceProperties.getPassword());
		//		mysqlXaDataSource.setAllowMultiQueries(true);
		//		mysqlXaDataSource.setLogXaCommands(true);ssss
		oracleXADataSource.setPinGlobalTxToPhysicalConnection(true);
		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
		xaDataSource.setXaDataSource(oracleXADataSource);
		xaDataSource.setUniqueResourceName("datasourceT100");
		xaDataSource.setMaxPoolSize(20);
		//		xaDataSource.setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
		return xaDataSource;
	}

	@Bean(name = "t100EntityManager")
	public LocalContainerEntityManagerFactoryBean t100EntityManager() throws Throwable {

		HashMap<String, Object> properties = new HashMap<String, Object>();
		properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
		properties.put("javax.persistence.transactionType", "JTA");
		//properties.put("hibernate.default_schema", otherDatasourceProperties.getDefault_schema());
		System.out.println("schema:"+otherDatasourceProperties.getDefault_schema());

		LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
		entityManager.setJtaDataSource(t100DataSource());
		entityManager.setJpaVendorAdapter(jpaVendorAdapter);
		entityManager.setPackagesToScan("cc.ucanuup.service.demo.entity");
		entityManager.setPersistenceUnitName("orderPersistenceUnit");
		entityManager.setJpaPropertyMap(properties);
		return entityManager;
	}

}
